package com.huidu.bitrade.controller.system;

import com.huidu.bitrade.constant.PageModel;
import com.huidu.bitrade.dao.SystemConfigDao;
import com.huidu.bitrade.entity.QSysPermission;
import com.huidu.bitrade.entity.QSystemConfig;
import com.huidu.bitrade.entity.SysPermission;
import com.huidu.bitrade.entity.SystemConfig;
import com.huidu.bitrade.mapper.SystemConfigMapper;
import com.huidu.bitrade.pagination.PageResult;
import com.huidu.bitrade.service.SystemConfigService;
import com.huidu.bitrade.util.MessageResult;
import com.querydsl.core.types.dsl.BooleanExpression;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/config")
@RequiredArgsConstructor
@Api(tags = "参数配置控制器")
public class SystemConfigController {

    private final SystemConfigService systemConfigService;
    private final SystemConfigMapper systemConfigMapper;
    @ApiOperation("保存配置")
    @PostMapping("save")
    public MessageResult save(SystemConfig systemConfig){
        systemConfigMapper.insert(systemConfig);
        return MessageResult.success();
    }

    @ApiOperation("修改配置")
    @PutMapping("update")
    public MessageResult update(SystemConfig systemConfig){
        systemConfigMapper.updateById(systemConfig);
        return MessageResult.success();
    }

    @ApiOperation("分页查询参数配置")
    @GetMapping("page")
    public MessageResult<PageResult<SystemConfig>> page(PageModel pageModel, @RequestParam(value = "keyword", required = false) String keyword){
        BooleanExpression predicate = null;
        if (StringUtils.isNotEmpty(keyword)) {
            predicate = QSystemConfig.systemConfig.configName.like(keyword);
        }
        Page<SystemConfig> all = systemConfigService.getPage(predicate, pageModel.getPageable());
        return MessageResult.success("查询成功",new PageResult<>(all));
    }
}
